package com.jstyle.jclifexd.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.ScreenUtils;
/**
 * Created by wyl on 2019/10/31.
 */

public class BottomDialog{
    private AlertDialog mDialog;
    private Activity mycontext;
    private View mView;

    public void setmView(View mView) {
        this.mView = mView;
    }

    public BottomDialog(Activity context, int resId) {
        if (context == null || resId <= 0) return;
        mycontext=context;
         mView =  LayoutInflater.from(context).inflate(resId, null);
        mDialog = new AlertDialog.Builder(context).create();
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(true);
        if (mView == null) return;
        setmView(mView);

    }

    /**
     * 设置当前dialog高度
     * @param activity
     * @param height
     */
    public void setWithandHeight(Activity activity,int height){
        if(null!=mDialog){
            //一定得在show完dialog后来set属性
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = ScreenUtils.getScreenWidth(activity);
            lp.height = height;
            mDialog.getWindow().setAttributes(lp);
        }
    }


    public View getmView(){
        return mView;
    }

    public boolean isshoeww(){
        return mDialog.isShowing();
    }

    public void show() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.setView(getmView());
            mDialog.show();
            //mDialog.setContentView(getmView());
            Window window = mDialog.getWindow();
            window.setWindowAnimations(R.style.mystyle); // 添加动画
            if (window != null) {
                window.setGravity(Gravity.BOTTOM);
                window.getDecorView().setPadding(0, 0, 0, 0); //为dialog里面的view设置padding
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.horizontalMargin = 0;
                mDialog.getWindow().setAttributes(params);
            }
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }




}
